package com.wx.sakura.service;

import java.util.*;

/**
 * @ClassName algorithmTest
 * @Description: TODO
 * @Author 86185
 * @Date 2020/8/17
 * @Version V1.0
 **/
public class AlgorithmTest {

    public List<Integer> intersection(int[] array1,int[] array2){
        List<Integer> list = new ArrayList<>();
        //构建2个数组的map
        Map<Integer,Integer> map1 = buildMap(array1);
        Map<Integer,Integer> map2 = buildMap(array2);
        for (Map.Entry<Integer,Integer> entry:map1.entrySet()) {
                int num = entry.getKey();
                if(map2.get(num)!=null){
                    list.add(num);
                }
        }
        return list;
    }
    public Map<Integer,Integer> buildMap (int[] array ){
        Map<Integer,Integer> resultMap = new HashMap<>();
        for (int i=0;i<array.length;i++){
            int num =    array[i];
            if(resultMap.get(num)!=null){
                resultMap.put(num,resultMap.get(num)+1);
            }else{
                resultMap.put(num,1);
            }
        }
        return resultMap;
    }
    //两个数组的交集
    public static void main(String[] args) {
        int[] numArr1 = new int[]{4,9,5};
        int[] numArr2 = new int[]{9,4,9,8,4};
        AlgorithmTest  a= new AlgorithmTest();
        List<Integer> list = a.intersection(numArr1,numArr2);
        for (int i:list
             ) {
            System.out.println(i);
        }
    }
}
